{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 理论部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAEJCAYAAACXCJy4AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3dfVRU9b4/8DfIoCA+oAGTJMebHR8iKuUU6sHLARWBYYSIVWJXOqUevXXiXO9R8yoKKma6ShbpyRNptrz2YFaIVD5esp+BP67SSTw/0zypB1EYcURjYHBmmO/vD2PkeUBm9t4y79dardXMbOb7dpj9Yc+e7/5+3IQQAkRE1Ou5yx2AiIikwYJPROQiWPCJiFwECz4RkYtgwScichEs+ERELsJD7gBEjlZZWYlnnnkG+fn5GDJkCADg4sWLWL58OWpqauDt7Y3169dj5MiRDhvzyJEjePPNN2EymTB69Gi89tpr8PHxabNdfn4+tm3bBjc3N3h5eWH58uUICQkBACQlJaGhoQEqlQoAoNVqMXfuXIdlJIIg6kXy8vJEZGSkGDVqlNDr9bb7n376abF3714hhBBHjhwRGo1GWK1Wh4yp1+vFhAkTxIULF4QQQmzYsEFkZGS02e6nn34Sv/3tb4VOp7PliIiIEEIIUVdXJ0JDQ4XJZHJIJqL28AifFKmkpATZ2dkYPnw4zp07B4vFglWrVmHfvn04fvx4i209PT2xe/du6HQ6HD58GNu2bUNMTIztcZ1Oh/Pnz0Oj0QAAIiIisGrVKpw+fRrBwcEtnisqKgoajQZFRUWora3FCy+8gFmzZqG4uBjr169vk3PRokWoqalBSEgIRowYAQBISUlBQkICMjIy4Obm1iJnVlYW/P39AQCPPPIIrl27BpPJhLKyMnh7e2Pu3Lm4fv06Jk6ciP/8z/9Ev379HPJ6EgE8pUMKV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oIFn4jIRfx/mHNAE5UzoaAAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from scipy.stats import binom\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn\n",
    "# seaborn.set()\n",
    "\n",
    "fig, ax = plt.subplots(3, 1)\n",
    "params = [(10, 0.25), (10, 0.5), (10, 0.8)]\n",
    "x = range(0, 11)\n",
    "\n",
    "for i in range(len(params)):\n",
    "    binom_rv = binom(n=params[i][0], p=params[i][1])\n",
    "    ax[i].set_title('n={},p={}'.format(params[i][0], params[i][1]))\n",
    "    ax[i].plot(x, binom_rv.pmf(x), 'bo', ms=8)\n",
    "    ax[i].vlines(x, 0, binom_rv.pmf(x), colors='b', lw=3)\n",
    "    ax[i].set_xlim(0, 10)\n",
    "    ax[i].set_ylim(0, 0.35)\n",
    "    ax[i].set_xticks(x)\n",
    "    ax[i].set_yticks([0, 0.1, 0.2, 0.3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 练习部分\n",
    "\n",
    "python实现二项分布，协方差和相关系数以及贝叶斯公式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二项分布 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "P\\{X=k\\}=C_n^kp^kq^{n-k}\n",
    "\\\\\n",
    "C_n^k = \\frac{n!}{k!(n-k)!}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def factorial(n):\n",
    "    \"\"\" 递归方式实现n的阶乘 \"\"\"\n",
    "    if n == 0:\n",
    "        return 1\n",
    "    else:\n",
    "        return n * factorial(n-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factorial(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def binomial_distribution(n, k, p):\n",
    "    \"\"\"\n",
    "    计算二项分布n次独立重复试验中事件A出现k次的概率\n",
    "    :param n: int, n次重复独立试验\n",
    "    :param k: int, 事件A成功k次\n",
    "    :param p: float, 事件A成功的概率\n",
    "    :return: float, n次试验中成功k次的概率\n",
    "    \"\"\"\n",
    "    C = factorial(n) / (factorial(k) * factorial(n-k))\n",
    "    q = 1 - p\n",
    "    return C * pow(p, k) * pow(q, n-k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.25"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "binomial_distribution(2, 2, 0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 10\n",
    "p = 0.5\n",
    "ks = [i for i in range(11)]\n",
    "probability = [binomial_distribution(n, k, p) for k in ks]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.LineCollection at 0x7fd5f4c35190>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAskAAAD4CAYAAAAejHvMAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29m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xN8n2D4/db8ObMaXYUuHfw/F7gX3RYy/ds67qArlXVxST3A5+if/fs41X1VMdlaf29CfhJ4L8k+dKg7e9X1Sc6rEnSq/fTwK8NTm6cBv5ax/VoHVXV55M8CXyB/uxEX2QCf7ZYNy7Jx4C/ANySZAVYAj4MfDzJB+j/D9J7uqvwFf4stSRJktTwcgtJkiSpYUiWJEmSGoZkSZIkqWFIliRJkhqGZEmSJKlhSJYkSZIahmRJkiSp8f8BFSUPyIDcXSYAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 864x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(12, 4))\n",
    "plt.plot(ks, probability, 'bo', ms=8)\n",
    "plt.vlines(ks, 0, probability, colors='b', lw=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 协方差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "Cov(X, Y) = E\\{  [X-E(X)] [Y-E(Y)]\\}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cov(X, Y):\n",
    "    \"\"\" 计算随机变量X与Y的协方差 \"\"\"\n",
    "    E_X = np.mean(X)\n",
    "    E_Y = np.mean(Y)\n",
    "    return np.mean((X - E_X)*(Y - E_Y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17.014075432218103"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X, Y = data1, data2\n",
    "E_X = np.mean(X)\n",
    "E_Y = np.mean(Y)\n",
    "\n",
    "np.mean((X - E_X)*(Y - E_Y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.816496580927726"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pearson相关系数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\rho(X,Y) = \\frac{Cov(X，Y)}{\\sqrt {Var(X)} \\sqrt {Var(Y)}}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 随机生成数据\n",
    "data1 = pd.Series(np.random.rand(100)*100).sort_values()\n",
    "data2 = pd.Series(np.random.rand(100)*50).sort_values()\n",
    "data = pd.DataFrame({'value1':data1.values,\n",
    "                     'value2':data2.values})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>value1</th>\n",
       "      <th>value2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>0.218303</td>\n",
       "      <td>0.644839</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2.200249</td>\n",
       "      <td>1.092248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2.548129</td>\n",
       "      <td>1.365964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2.824168</td>\n",
       "      <td>1.620999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>3.075062</td>\n",
       "      <td>1.817107</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     value1    value2\n",
       "0  0.218303  0.644839\n",
       "1  2.200249  1.092248\n",
       "2  2.548129  1.365964\n",
       "3  2.824168  1.620999\n",
       "4  3.075062  1.817107"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>value1</th>\n",
       "      <th>value2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>value1</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.991726</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>value2</td>\n",
       "      <td>0.991726</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          value1    value2\n",
       "value1  1.000000  0.991726\n",
       "value2  0.991726  1.000000"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调包计算相关系数\n",
    "data.corr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.03833972716424944"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cov(data1, data2) / (np.std(data1) * np.std(data2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 贝叶斯公式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
